package com.czk.stream;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.DoubleSummaryStatistics;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

import static java.lang.Double.parseDouble;

/**
 * @Author:ChenZhangKun
 * @Date: 2024/3/12 22:28
 */
public class FileRead {
    public static void main(String[] args) throws FileNotFoundException {

        Map<String, DoubleSummaryStatistics> collect = new BufferedReader(new FileReader("E:\\czk\\czk.txt"))
                .lines()
                .parallel()
                .collect(Collectors.groupingBy(line -> line.substring(0, line.indexOf(';')),
                        Collectors.summarizingDouble(line -> parseDouble(line.substring(line.indexOf(';') + 1)))
                ));
        TreeMap<String, String> result = collect.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> {
            DoubleSummaryStatistics doubleSummaryStatistics = e.getValue();
            return String.format("%.1f/%.0f/%.1f", doubleSummaryStatistics.getMin(), doubleSummaryStatistics.getAverage(), doubleSummaryStatistics.getMax());
        }, (l, r) -> r, TreeMap::new));
        System.out.println(result);
    }
}
